package com.szcinda.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface GhOrderRepository extends JpaRepository<GhOrder, String>, JpaSpecificationExecutor<GhOrder> {
    GhOrder findById(String id);

    long countByBusinessCode(String businessCode);

    GhOrder findFirstByOrganizationIdAndBookNumber(String organizationId, String bookNumber);

    List<GhOrder> findByBookNumberIn(List<String> bookNumbers);

    List<GhOrder> findAllByOrderNumberIn(List<String> orderNumberList);

    long countByOrganizationNameAndClientNameAndBookNumberAndBusinessCode(String organizationName, String clientName, String bookNumber, String businessCode);

    GhOrder findByOrderNumber(String orderNumber);

    List<GhOrder> findByOrderNumberIn(List<String> orderNumbers);

    @Query("select z from GhOrder z where (z.invoice = ?1 or z.invoice is null) and z.organizationId in (?2)")
    List<GhOrder> findInvoice(String invoice, List<String> orgIds);

    @Query("select z from GhOrder z where (z.outInvoice = ?1 or z.outInvoice is null) and z.organizationId in (?2)")
    List<GhOrder> findOutInvoice(String invoice, List<String> orgIds);

    @Query("select z from GhOrder z where (z.settlement = ?1 or z.settlement is null) and z.organizationId in (?2)")
    List<GhOrder> findSettlement(String settlement, List<String> orgIds);

    @Query("select z from GhOrder z where (z.outSettlement = ?1 or z.outSettlement is null) and z.organizationId in (?2)")
    List<GhOrder> findOutSettlement(String settlement, List<String> orgIds);

    List<GhOrder> findByOrganizationId(String organizationId);
}
